os.unloadAPI("sensors");
os.unloadAPI("sensorsUI");
os.loadAPI("/rom/apis/sensors");
os.loadAPI("/rom/apis/sensorsUI");
local tArgs={...};
print("APIs loaded")

--rednet.open("left") -- Open wireless modem on RH side for communication to base console
targetVDU = peripheral.wrap("top")

function printAt(x, y, text)
	targetVDU.setCursorPos(x,y)
	targetVDU.write(text)
end

function stringContains(aString, aSearch)
	local res = string.find(aString, aSearch)
	if res and res > 0 then
		return true
	else
		return false
	end
end

-- Searches a table of index/value pairs, searching for the specified string within any value.
-- Returns the index of the first find, or nil if not found
function getIndexOf(aTable, aSearchString)
	for key,value in ipairs(aTable) do
		if stringContains(tostring(value), aSearchString) then
			return key
		end
	end
	return nil
end

printAt(1,1,"Console running...")

mySensors = sensors.getSensors("right")
print("Sensor : "..mySensors[1])
myProbe = sensors.getProbes("right",mySensors[1])
print("Probes : "..table.concat(myProbe,"|"))
myTargets = sensors.getAvailableTargetsforProbe("right",mySensors[1],myProbe[3])
iMFSU = getIndexOf(myTargets, "MFSU")
print("MFSU found at index ["..tostring(iMFSU).."]")
myReadings = sensors.getSensorReadingAsTable("right",mySensors[1],myTargets[iMFSU],myProbe[3])
print("Readings : ")
for i,v in ipairs(myReadings) do
	print(tostring(i).."="..tostring(v))
end

myReading = sensors.getSensorReadingAsDict("right",mySensors[1],myTargets[4],myProbe[3])
print("Max Storage : "..tostring(myReading.maxStorage))
print("Current     : "..tostring(myReading.energy))

while true do
	redstone.setBundledOutput("back", colors.white)
	printAt(1,2,"White active")
	sleep(10)
	redstone.setBundledOutput("back", colors.black)
	printAt(1,2,"Black active ")
	sleep(10)
end

--rednet.close("left")

